﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Facebook;
using System.Windows.Threading;
using System.IO;
using System.Collections.ObjectModel;
using FBApp.Classes;

namespace FBApp
{
	public class NewsFeedItem
	{
		public string id {get; set;}
		public string fromName {get;set;}
		public string fromID {get;set;}
		public string message {get;set;}

		public string createdTime {get;set;}
		public string updatedTime {get;set;}
		//comments list
		public feedType type {get;set;}
		public attribution attrib {get;set;}
        public ImageSource Picture { get; set; }
        public ImageSource AttributePicture { get; set; }
		
        //Links
        public string linkTitle { get; set; }
        public ImageSource linkPicture { get; set; }
        public string linkDescription { get; set; }

        public Visibility linkAdded { get; set; }

        public ObservableCollection<CommentItem> comments { get; set; }

		public enum attribution
		{
            iphone,
            android,
			blackberry,
            mobileweb,
            rssgraffiti
		}
		
		public enum feedType
		{
			status,
			link
		}
						
		public NewsFeedItem()
		{
						
		}
		
		public NewsFeedItem(string id,string fromName,string fromID,string message,string createdTime,string type,string attrib)
		{
            comments = new ObservableCollection<CommentItem>();
			this.id = id;
			this.fromName = fromName;
            this.fromID = fromID;
			this.message = message;
			this.createdTime = createdTime;
            this.linkAdded = Visibility.Collapsed;

            BitmapImage bi = new BitmapImage();

			if(type.Equals("status"))
			{
				this.type = feedType.status;
			}
			else if(type.Equals("link"))
			{
				this.type = feedType.link;
			}

            if (attrib.ToLower().Equals("blackberry"))
            {
                this.attrib = attribution.blackberry;
                Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate()
                {
                    bi = new BitmapImage(new Uri("/Resources/Images/Icons/Blackberry.gif", UriKind.Relative));
                    AttributePicture = bi;
                }
                ));
                this.createdTime = createdTime + " via BlackBerry.";
            }
            else if (attrib.ToLower().Equals("mobile web"))
            {                
                this.attrib = attribution.mobileweb;
                Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate()
                {
                    bi = new BitmapImage(new Uri("/Resources/Images/Icons/mobile_web.gif", UriKind.Relative));
                    AttributePicture = bi;
                }));
                this.createdTime = createdTime + " via Mobile Web.";
            }else if(attrib.ToLower().Equals("iphone"))                
            {
                this.attrib = attribution.iphone;
                Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate()
                {
                    bi = new BitmapImage(new Uri("/Resources/Images/Icons/mobile_web.gif", UriKind.Relative));
                    AttributePicture = bi;
                }));
                this.createdTime = createdTime + " via Iphone.";
            }
            else if (attrib.ToLower().Equals("android"))
            {
                this.attrib = attribution.android;
                Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate()
                {
                    bi = new BitmapImage(new Uri("/Resources/Images/Icons/mobile_web.gif", UriKind.Relative));
                    AttributePicture = bi;
                }));
                this.createdTime = createdTime + " via Android.";
            }   
            else if (attrib.ToLower().Equals("rss graffiti"))
            {
                this.attrib = attribution.mobileweb;
                Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate()
                {
                    bi = new BitmapImage(new Uri("/Resources/Images/Icons/rss_graffiti.gif", UriKind.Relative));
                    AttributePicture = bi;
                }));
                this.createdTime = createdTime + " via RSS Graffiti.";
            } 
            
            string urlString = "http://graph.facebook.com/" + fromID + "/picture";

            Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate()
            {
                bi = SharedFunctions.loadImage(fromID);
                this.Picture = bi;
            }
            ));
		}        

        public void addLink(string linkTitle,string linkPicture,string linkDescription)
        {
            this.linkTitle = linkTitle;
            this.linkDescription = linkDescription;
            linkAdded = Visibility.Visible;

            BitmapImage bi = new BitmapImage();

            Application.Current.Dispatcher.Invoke(DispatcherPriority.Normal, new Action(delegate()
            {
                if (!linkPicture.Equals(""))
                {
                    bi = new BitmapImage(new Uri(linkPicture, UriKind.Absolute));
                    this.linkPicture = bi;
                }                
            }
            ));
        }

        public void addComment(CommentItem ci)
        {
            if (comments != null)
            {
                comments.Add(ci);
            }
        }
		
	}
}